package com.kingbook.api.rest;

import com.kingbook.common.utils.StringUtils;
import com.kingbook.core.annotations.ContainProperties;
import com.kingbook.core.auth.LoginManager;
import com.kingbook.core.result.JSONResult;
import com.kingbook.services.MasterService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static com.kingbook.core.auth.LoginManager.AUTH;


/**
 * @Author Bucky
 * @Date 2019-12-20 21:28:56
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("master")
public class MasterRest {

    @Autowired
    MasterService masterService;

    @PostMapping
    public JSONResult modify(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        param.put("id", LoginManager.getMid(auth));
        log.info("---->{}", param);
        return masterService.modify(param);
    }

    @GetMapping
    public JSONResult find(@RequestHeader(name = AUTH) String auth) {
        int mid = LoginManager.getMid(auth);
        log.info("---->mid={}", mid);
        return masterService.find(mid);
    }

    @PostMapping("query")
    public JSONResult query(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        param.put("id", LoginManager.getMid(auth));
        log.info("---->{}", param);
        return masterService.query(param);
    }

    @PostMapping("pages")
    @ContainProperties({"pageNo", "pageSize"})
    public JSONResult queryForPages(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        param.put("mid", LoginManager.getMid(auth));
        log.info("---->{}", param);
        return masterService.queryForPages(param);
    }

    @PostMapping("confirmPartner")
    @ContainProperties({"phone1", "phone2", "phone3", "shortNumber", "referralCode"})
    public JSONResult confirmPartner(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        int mid = LoginManager.getMid(auth);
        log.info("---->mid={} {}", mid, param);
        if(StringUtils.isAllEmpty(MapUtils.getString(param, "phone1", ""),
                MapUtils.getString(param, "phone2", ""),
                MapUtils.getString(param, "phone3", ""),
                MapUtils.getString(param, "shortNumber", ""),
                MapUtils.getString(param, "referralCode", ""))) {
            return JSONResult.failed("推荐码】【短号】【手机号】必须填写一个才能查找");
        }
        return masterService.confirmPartner(param, mid);
    }
}
